/**
 * 反馈模块的Vuex Store
 * @module store/modules/feedback
 * @description 管理用户反馈相关的状态
 */
import { submitFeedback, uploadFeedbackImage, getFeedbackHistory } from '@/api/feedback';

// 初始状态
const state = {
  // 反馈类型列表
  feedbackTypes: [
    { 
      label: '功能问题', 
      value: 'function',
      icon: '/static/images/common/icon-function.png',
      activeIcon: '/static/images/common/icon-function-active.png'
    },
    { 
      label: '体验建议', 
      value: 'experience',
      icon: '/static/images/common/icon-experience.png',
      activeIcon: '/static/images/common/icon-experience-active.png'
    },
    { 
      label: '内容优化', 
      value: 'content',
      icon: '/static/images/common/icon-content.png',
      activeIcon: '/static/images/common/icon-content-active.png'
    },
    { 
      label: '其他', 
      value: 'others',
      icon: '/static/images/common/icon-others.png',
      activeIcon: '/static/images/common/icon-others-active.png'
    }
  ],
  // 反馈历史记录
  feedbackHistory: [],
  // 提交状态
  submitting: false,
  // 上传图片状态
  uploading: false
};

// getters
const getters = {
  // 获取反馈类型列表
  feedbackTypes: state => state.feedbackTypes,
  // 获取反馈历史记录
  feedbackHistory: state => state.feedbackHistory,
  // 获取提交状态
  submitting: state => state.submitting,
  // 获取上传状态
  uploading: state => state.uploading
};

// mutations
const mutations = {
  // 设置反馈历史记录
  SET_FEEDBACK_HISTORY(state, history) {
    state.feedbackHistory = history;
  },
  // 设置提交状态
  SET_SUBMITTING(state, status) {
    state.submitting = status;
  },
  // 设置上传状态
  SET_UPLOADING(state, status) {
    state.uploading = status;
  },
  // 添加一条新的反馈到历史记录
  ADD_FEEDBACK(state, feedback) {
    state.feedbackHistory.unshift(feedback);
  }
};

// actions
const actions = {
  /**
   * 提交反馈
   * @param {Object} context - Vuex上下文
   * @param {Object} feedbackData - 反馈数据
   * @returns {Promise} 提交结果
   */
  async submitFeedback({ commit }, feedbackData) {
    try {
      commit('SET_SUBMITTING', true);
      const res = await submitFeedback(feedbackData);
      if (res.code === 200 || res.code === 0) {
        // 如果提交成功，可以将新反馈添加到历史记录中
        const newFeedback = {
          ...feedbackData,
          id: res.data.feedbackId,
          submitTime: res.data.submitTime,
          status: res.data.status
        };
        commit('ADD_FEEDBACK', newFeedback);
        return Promise.resolve(res.data);
      } else {
        return Promise.reject(res.message || '提交反馈失败');
      }
    } catch (error) {
      console.error('提交反馈失败', error);
      return Promise.reject(error);
    } finally {
      commit('SET_SUBMITTING', false);
    }
  },

  /**
   * 上传反馈图片
   * @param {Object} context - Vuex上下文
   * @param {Object} file - 文件对象
   * @returns {Promise} 上传结果
   */
  async uploadFeedbackImage({ commit }, file) {
    try {
      commit('SET_UPLOADING', true);
      const res = await uploadFeedbackImage(file);
      if (res.code === 200 || res.code === 0) {
        return Promise.resolve(res.data);
      } else {
        return Promise.reject(res.message || '上传图片失败');
      }
    } catch (error) {
      console.error('上传图片失败', error);
      return Promise.reject(error);
    } finally {
      commit('SET_UPLOADING', false);
    }
  },

  /**
   * 获取反馈历史记录
   * @param {Object} context - Vuex上下文
   * @param {Object} params - 查询参数
   * @returns {Promise} 查询结果
   */
  async getFeedbackHistory({ commit }, params = {}) {
    try {
      const res = await getFeedbackHistory(params);
      if (res.code === 200 || res.code === 0) {
        commit('SET_FEEDBACK_HISTORY', res.data.list || []);
        return Promise.resolve(res.data);
      } else {
        return Promise.reject(res.message || '获取反馈历史失败');
      }
    } catch (error) {
      console.error('获取反馈历史失败', error);
      return Promise.reject(error);
    }
  }
};

export default {
  namespaced: true,
  state,
  getters,
  mutations,
  actions
};
